﻿using LPBToolsLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 电子票存档工具
{
    /// <summary>
    /// 数据库地址：192.168.0.3
    /// 数据库名称：HCEBills
    /// 数据库用户：HCEBillsUser
    /// 数据库密码：EBillUser
    /// </summary>
    public partial class FLogin : Form
    {
        AppRunConfig DBServerCFG { get; }
        SQLSerHelper tDBSQLSer { get; }
        /// <summary>
        /// 程序当前运行的目录
        /// </summary>
        public string currentDirectory { get; private set; }

        /// <summary>
        /// 用于Zip文件解压的临时目录
        /// </summary>
        public const string tempPath = "temp\\";

        public List<string> sFilesList { get; } = new List<string>();
        public FLogin(AppRunConfig DestDBServerCFG)
        {
            InitializeComponent();
            currentDirectory = Directory.GetCurrentDirectory() + "\\";
            if (!Directory.Exists(currentDirectory + tempPath))
            {
                Directory.CreateDirectory(currentDirectory + tempPath);
            }
            DBServerCFG = DestDBServerCFG ;
            //DBServerCFG.Address = "192.168.0.3";
            //DBServerCFG.DBUser = "HCEBillsUser";
            //DBServerCFG.DBPass = "EBillUser";
            //DBServerCFG.DBName = "HCEBills";
            if (!string.IsNullOrEmpty(DBServerCFG.lastLoginPass))
            {
                textBoxLoginUser.Text = DBServerCFG.lastLoginUser;
                textBoxLoginPass.Text = DBServerCFG.lastLoginPass;
            }
            tDBSQLSer = new SQLSerHelper(DBServerCFG);
            string sInfo = DBServerCFG.ConnectStr;
            this.DialogResult = DialogResult.No;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            FUploadEBills tUploadEBills = new FUploadEBills(DBServerCFG);
            tUploadEBills.ShowDialog();
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if ((!string.IsNullOrEmpty(textBoxLoginUser.Text))
                && (!string.IsNullOrEmpty(textBoxLoginPass.Text)))
            {
                string sSQL = "select count(*) from EBillsUser where LoginName='{0}' and LoginPass='{1}' and UserLevel>{2}";
                sSQL = string.Format(sSQL, textBoxLoginUser.Text, textBoxLoginPass.Text, '0');
                object tResult = tDBSQLSer.runSelect(sSQL);
                if (tResult.ToString().Equals("1"))
                {
                    DBServerCFG.lastLoginUser = textBoxLoginUser.Text;
                    DBServerCFG.lastLoginPass = textBoxLoginPass.Text;
                    this.DialogResult = DialogResult.Yes;
                    Close();
                }
                else
                {
                    MessageBox.Show("登录失败，请检查用户名和密码！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("用户名和密码不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonConfig_Click(object sender, EventArgs e)
        {
            FDBSerConfig tDBSerConfig = new FDBSerConfig(DBServerCFG);
            tDBSerConfig.ShowDialog();
        }
    }
}
